package com.hmo.appshop.mamager.controller;

import com.hmo.appshop.mamager.bean.App;
import com.hmo.appshop.mamager.dto.commom.Result;
import com.hmo.appshop.mamager.service.AppService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("app")
public class AppController  {

    @Autowired
    AppService appService;
    @Value("${image}")
    private String image;

    @PostMapping(value = "/apk")
    @ApiOperation("提交apk解析并写入app")
    private Result apk(@RequestParam(value = "file") MultipartFile file){
        System.out.println("file = " + file.getOriginalFilename());
        if (file.isEmpty()){
            return new Result().fail("文件为空");
        }
        App app = appService.apk(file);
        if (app==null)
            return new Result().fail("解析失败");
        return new Result().ok(app);
    }

    @PutMapping("/")
    @ApiOperation("更新app")
    private Result putApp(@RequestBody App app){
        App update = appService.update(app);
        if (update==null)
            return new Result().fail("更新失败");
        return new Result().ok(update);
    }


    @PostMapping(value = "/logo")
    @ApiOperation("提交并删除原logo")
    private Result logo(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "id") int id){
        App s = appService.updateLogo(file, id);
        return new Result().ok(s);
    }

    @PostMapping("/picture")
    @ApiOperation("提交picture")
    private Result picture(@RequestParam(value = "file") MultipartFile multipartFile,@RequestParam(value = "id") int id){
        String s = appService.addPicture(multipartFile, id);
        if (s==null)
            return new Result().fail("上传失败");
        return new Result().addCodeMessage(200,"succeed",s);
    }

    @DeleteMapping("/picture")
    @ApiOperation("删除picture")
    private Result delpicture(String picture,int id){
        if (picture==null){
            return new Result().fail("图片名未空");
        }
        String s = appService.delPicture(picture, id);
        return new Result().addCodeMessage(200,"succeed",s);
    }

    @RequestMapping(value = "/image/{type}/{imageName}",method = RequestMethod.GET,produces = MediaType.IMAGE_PNG_VALUE)
    @ApiOperation("获取图片,type：icon or picture")
    public byte[] ShowImg(@PathVariable("imageName") String imageName,@PathVariable("type") String path) throws IOException {
        String imagePath=image;
        File file = new File(imagePath+imageName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        inputStream.close();
        return bytes;
    }


    @GetMapping("/type/{type}")
    @ApiOperation("查询所有")
    public Result selectAll(@PathVariable("type") Integer type){
        if (type==null)
            type=0;
        List<App> apps = appService.selectApp(type);
        return new Result().ok(apps);
    }

    @GetMapping("/id")
    @ApiOperation("查询单个")
    public App selectOne(Integer id){
        App app = appService.selectOne(id);
        return app;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除app")
    private Result delApp(@PathVariable("id") Integer id){
        if (id==null){
            return new Result().fail("错误");
        }
        int i = appService.delApp(id);
        if (i==1)
            return new Result().ok();
        return new Result().fail("未知错误");
    }
}

